package org.example.socket;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {
    public static void main(String[] args) throws Exception {
        try (ServerSocket serverSocket = new ServerSocket(8899)) {
            System.out.println("socket server start");
            while (true) {
                Socket socket = serverSocket.accept();
                InputStream inputStream = socket.getInputStream();
                OutputStream outputStream = socket.getOutputStream();
                // 接收客户端消息
                byte[] message = new byte[2048];
                int len = inputStream.read(message);
                System.out.println("接收到客户端消息：" + new String(message, 0, len));
                // 发送消息到客户端
                String sendMsg = "server send massage";
                outputStream.write(sendMsg.getBytes());
                System.out.println("发送到客户端消息: " + sendMsg);
    //            socket.close();
            }
        }
    }
}
